﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_Pages_UserControls_EditAccount : System.Web.UI.UserControl
{
    private AdminBLL adminBLL;
    private Users user;
    private string username;

    protected void Page_Load(object sender, EventArgs e)
    {
        
        this.adminBLL = new AdminBLL();
        
        this.username = Request.QueryString["Username"];
        
        txtUsername.Text = username;
        
        this.user = adminBLL.GetIUsers.GetUserByUsername(username);
        
        txtEmail.Text = this.user.Email.ToString();
        txtNgaytao.Text = this.user.CreateTime.ToString();

        if (this.user.Active)
        {
            this.rbHoatDong.Checked = true;
        }
        else 
        {
            this.rbKhoa.Checked = true;
        }

        if (!Page.IsPostBack)
        {
            BindCob();
        }
    }

    void BindCob()
    {
        List<Roles> listroles = this.adminBLL.GetIRoles.GetAllRoles();

        this.cobQuyen.DataTextField = "RoleName";
        this.cobQuyen.DataValueField = "IdRole";

        this.cobQuyen.DataSource = listroles;
        this.cobQuyen.DataBind();
        
        this.cobQuyen.SelectedValue = this.user.Role.IdRole.ToString();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {

        this.username = Request.QueryString["Username"];

        if (rbKhoa.Checked)
        {
            this.user.Active = false;

        }
        else
        {
            this.user.Active = true;
        }

        int idRole = int.Parse(this.cobQuyen.SelectedValue);
        Roles role = this.adminBLL.GetIRoles.GetRoleById(idRole);
        this.user.Role = role;
         
        bool kq = adminBLL.GetIUsers.UpdateUser(user);
        if (kq == true)
        {
            Response.Write("<script>alert('Cap nhat thanh cong')</script>");
            Response.Redirect("ShowListAccount.aspx");
        }
        else
        {
            Response.Write("<script>alert('That bai')</script>");
            Response.Redirect("Fail.aspx");
        }
     
    }

    protected void rdTrangThai_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void cobQuyen_SelectedIndexChanged(object sender, EventArgs e)
    {
     
        
    }
}